package org.example.mine.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.mine.config.LocalDateTimeUtil;
import org.example.mine.mapper.BillMapper;
import org.example.mine.service.BillService;
import org.example.model.domain.mine.Bill;
import org.example.model.domain.mine.ExpressPaymentRecords;
import org.example.model.domain.mine.ExpressRechargeRecord;
import org.example.model.domain.mine.ExpressWithdraw;
import org.example.model.vo.mine.BillVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BillServiceImpl implements BillService {

    @Resource
    public BillMapper billMapper;
    @Override
    public BillVo selectBillList(Long registerIp) {
        List<ExpressWithdraw> bill=billMapper.selectBillList(registerIp);
        List<ExpressRechargeRecord> topup=billMapper.selecttopup(registerIp);
        List<Bill> bills = new ArrayList<>();
//        因为要查询两个不同表的信息 为了返回数据 所以定义一个字段统一的实体类 在根据日期进行分组展示
        for (ExpressWithdraw expressPaymentRecords : bill) {
            Bill bill1 = new Bill();
            bill1.setClassify("提现");
            bill1.setMoney(String.valueOf(expressPaymentRecords.getaMoney()));
            bill1.setOrderNo(String.valueOf(expressPaymentRecords.getOrderNo()));
            bill1.setPayWay(String.valueOf(expressPaymentRecords.getWithdrawWay()));
            bill1.setOverTime(expressPaymentRecords.getOverTime());
            bill1.setResidueBalance(String.valueOf(expressPaymentRecords.getBalanceAfterRecharging()));
            bills.add(bill1);
        }
        for (ExpressRechargeRecord expressRechargeRecord : topup) {
            Bill bill1 = new Bill();
            if(expressRechargeRecord.getRechargeChannels()==2){
                bill1.setClassify("支付宝充值");
            }else{
                bill1.setClassify("微信充值");
            }
            bill1.setMoney(String.valueOf(expressRechargeRecord.getRechargeAmount()));
            bill1.setOrderNo(expressRechargeRecord.getTransactionNumber());
            bill1.setPayWay(String.valueOf(expressRechargeRecord.getRechargeChannels()));
            bill1.setOverTime(expressRechargeRecord.getCreateTime());
            bill1.setResidueBalance(String.valueOf(expressRechargeRecord.getBalanceAfterRecharging()));
            bills.add(bill1);
        }
        BillVo billVo = new BillVo();
        if (bills.size()!=0) {
            LinkedHashMap<String, List<Bill>> map = bills.stream().sorted(Comparator.comparing(Bill::getOverTime).reversed()).collect(Collectors.groupingBy(b-> LocalDateTimeUtil.parse_yyyyMM(b.getOverTime()),LinkedHashMap::new,Collectors.toList()));
            log.info("map:"+map);
            billVo.setMap(map);
        }
        return billVo;
    }

    @Override
    public ExpressPaymentRecords getbillbyId(Long id) {
        log.info("查询成功:"+billMapper.getbillbyId(id));
        return billMapper.getbillbyId(id);
    }

    @Override
    public String getDailyIncome(Long registerIp) {
        DateTime begin = DateUtil.beginOfDay(new Date());
        DateTime end = DateUtil.endOfDay(new Date());
        BigDecimal num=billMapper.getDailyIncome(begin,end,registerIp);
        return String.valueOf(num);
    }
}
